#include "widget.h"
#include "ui_widget.h"
#include "m0wig.h"
#include "mbappwig.h"
#include "modbuswig.h"
#include "widget.h"
#include "setwig.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("智能网关");
    setWindowIcon(QIcon(":/智能家居.png"));

    //实例化
    M0Wig *wid1 = new M0Wig;         //客厅
    MbappWig *wid2 = new MbappWig;   //卧室
    ModbusWig *wid3 = new ModbusWig; //客房
    SetWig *wid4 = new SetWig;       //设置
    //加入堆栈窗体
    ui->stackedWidget->addWidget(wid1);
    ui->stackedWidget->addWidget(wid2);
    ui->stackedWidget->addWidget(wid3);
    ui->stackedWidget->addWidget(wid4);

    client = new QMQTT::Client();
    connect(wid4,SIGNAL(mySignal(QString)),this,SLOT(SetPushBtnCon(QString)));

    connect(client,SIGNAL(connected()),this,SLOT(doConnected()));
    connect(client,SIGNAL(subscribed(const QString&, const quint8)),this,SLOT(doSubscribed(const QString&, const quint8)));
    connect(client,SIGNAL(received(QMQTT::Message)),this,SLOT(doDataReceived(QMQTT::Message)));

    //模式更改
    connect(wid4,SIGNAL(sbmode1(int)),this,SLOT(SbmodeSlot(int)));
    connect(wid4,SIGNAL(sbmode2(int)),this,SLOT(SbmodeSlot(int)));
    connect(wid4,SIGNAL(sbmode3(int,int)),this,SLOT(zqsSlot(int,int)));

    connect(wid1,SIGNAL(shuaxin(int)),this,SLOT(sxsb(int)));
    connect(wid2,SIGNAL(shuaxin(int)),this,SLOT(sxsb(int)));
    connect(wid3,SIGNAL(shuaxin(int)),this,SLOT(sxsb(int)));
    //数据发回给子界面
    connect(this,SIGNAL(data1(QString)),wid1,SLOT(m0temperaturedata(QString)));
    connect(this,SIGNAL(data2(QString)),wid1,SLOT(m0humiditydata(QString)));
    connect(this,SIGNAL(data3(QString)),wid1,SLOT(m0illdata(QString)));
    connect(this,SIGNAL(data101(QString)),wid2,SLOT(modbustemperaturedata(QString)));
    connect(this,SIGNAL(data102(QString)),wid2,SLOT(modbushumiditydata(QString)));
    connect(this,SIGNAL(data201(QString)),wid3,SLOT(mbappilldata(QString)));
    connect(this,SIGNAL(data202(QString)),wid3,SLOT(mbapphumiditydata(QString)));
    connect(this,SIGNAL(huixian(QString)),wid4,SLOT(huixianshuju(QString)));
    //客厅控制
    connect(wid1,SIGNAL(m0light(int)),this,SLOT(livingroom_ktkz(int)));
    connect(wid1,SIGNAL(m0alarm(int)),this,SLOT(livingroom_jgkz(int)));
    connect(wid1,SIGNAL(m0fan(int)),this,SLOT(livingroom_fskz(int)));
    //卧室控制
    connect(wid2,SIGNAL(mbaswith(int)),this,SLOT(bedroom_ktkz(int)));
    connect(wid2,SIGNAL(mbalight(int)),this,SLOT(bedroom_dpkz(int)));
    connect(wid2,SIGNAL(mbatem(QString)),this,SLOT(bedroom_wdkz(QString)));
    //客房控制
    connect(wid3,SIGNAL(mobswith(int)),this,SLOT(gustroom_ktkz(int)));
    connect(wid3,SIGNAL(moblight(int)),this,SLOT(gustroom_dkz(int)));
    connect(wid3,SIGNAL(mobalarm(int)),this,SLOT(gustroom_jbkz(int)));
    connect(wid3,SIGNAL(mobtem(QString)),this,SLOT(gustroom_wdkz(QString)));

}
void Widget::SetPushBtnCon(QString mqttip)
{
    qDebug()<<mqttip;

    QHostAddress hostip(mqttip);
    client->setHost(hostip);
    client->setPort(1883);
    client->setClientId("clientid");
    client->connectToHost();
}

void Widget::doConnected()
{
    qDebug()<<"doConnected";
    client->subscribe("/app/data/up");
    client->subscribe("/app/control/up");
    client->subscribe("/app/mode/up");
}

//上报模式槽函数

void Widget::SbmodeSlot(int flag)
{
    if(flag==0)
    {
        QString topic ="/app/mode/down";
        QJsonObject dataObject;
        dataObject.insert("type",0);
        dataObject.insert("period",5);
        QJsonObject json;
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        client->publish(msg);

    }
    else
    {
        QString topic ="/app/mode/down";
        QJsonObject dataObject;
        dataObject.insert("period",5);
        dataObject.insert("type",1);
        QJsonObject json;
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        client->publish(msg);
    }
}
void Widget::zqsSlot(int flag,int value)
{
    QString topic = "/app/mode/down";
    QJsonObject dataObject;
    dataObject.insert("type",flag);
    dataObject.insert("period",value);
    QJsonObject json;
    json.insert("data",QJsonValue(dataObject));
    QJsonDocument document;
    document.setObject(json);
    QByteArray data = document.toJson(QJsonDocument::Compact);
    QMQTT::Message msg(0, topic.toLatin1(), data);
    client->publish(msg);
}

void Widget::sxsb(int index)
{
    QString topic = "/app/data/down";
    QJsonObject dataObject;
    dataObject.insert("type",index);
    dataObject.insert("limit","all");
    QJsonDocument document;
    document.setObject(dataObject);
    QByteArray data = document.toJson(QJsonDocument::Compact);
    QMQTT::Message msg(0,topic.toLatin1(),data);
    qDebug()<<data.data();
    client->publish(msg);

}

//客厅控制
void Widget::livingroom_ktkz(int ktlight)
{
    if(ktlight==0)
    {
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",4);
        dataObject.insert("val","1");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(),data);
        qDebug()<<"light_ctrl = "<<data.data();
        client->publish(msg);
    }
    else{
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",4);
        dataObject.insert("val","0");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"light_ctrl = "<<data.data();
        client->publish(msg);
    }
}

void Widget::livingroom_jgkz(int ktjg)
{
    if(ktjg==0)
    {
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",5);
        dataObject.insert("val","1");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"ill_ctrl = "<<data.data();
        client->publish(msg);
    }
    else{
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",5);
        dataObject.insert("val","0");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"ill_ctrl = "<<data.data();
        client->publish(msg);
    }
}

void Widget::livingroom_fskz(int ktfs)
{
    if(ktfs==0)
    {
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",6);
        dataObject.insert("val","1");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"fan_ctrl = "<<data.data();
        client->publish(msg);
    }
    else{
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",6);
        dataObject.insert("val","0");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"fan_ctrl = "<<data.data();
        client->publish(msg);
    }
}

//卧室控制
void Widget::bedroom_ktkz(int wskt)
{
    if(wskt==0)
    {
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",103);
        dataObject.insert("val","1");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"switch_ctrl = "<<data.data();
        client->publish(msg);
    }
    else{
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",103);
        dataObject.insert("val","0");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(),data);
        qDebug()<<"switch_ctrl = "<<data.data();
        client->publish(msg);
    }
}

void Widget::bedroom_dpkz(int wsdp)
{
    if(wsdp==0)
    {
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",105);
        dataObject.insert("val","1");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"light_ctrl = "<<data.data();
        client->publish(msg);
    }
    else{
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",105);
        dataObject.insert("val","0");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"light_ctrl = "<<data.data();
        client->publish(msg);
    }
}

void Widget::bedroom_wdkz(QString wendu)
{
    QString topic = "/app/control/down";
    QJsonObject json;
    json.insert("type",2);
    QJsonObject dataObject;
    dataObject.insert("key",104);
    dataObject.insert("val",wendu);
    json.insert("data",QJsonValue(dataObject));
    QJsonDocument document;
    document.setObject(json);
    QByteArray data = document.toJson(QJsonDocument::Compact);
    QMQTT::Message msg(0, topic.toLatin1(), data);
    qDebug()<<"switch_ctrl = "<<data.data();
    client->publish(msg);
}
//客房控制
void Widget::gustroom_ktkz(int kfkt)
{
    if(kfkt==0)
    {
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",203);
        dataObject.insert("val","1");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"kt_ctrl = "<<data.data();
        client->publish(msg);
    }
    else{
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",203);
        dataObject.insert("val","0");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"kt_ctrl = "<<data.data();
        client->publish(msg);
    }
}

void Widget::gustroom_dkz(int kfd)
{
    if(kfd==0)
    {
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",205);
        dataObject.insert("val","1");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"ktlight_ctrl = "<<data.data();
        client->publish(msg);
    }
    else{
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",205);
        dataObject.insert("val","0");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"kflight_ctrl = "<<data.data();
        client->publish(msg);
    }
}

void Widget::gustroom_jbkz(int kfjb)
{
    if(kfjb==0)
    {
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",206);
        dataObject.insert("val","1");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"alaarm_ctrl = "<<data.data();
        client->publish(msg);
    }
    else{
        QString topic = "/app/control/down";
        QJsonObject json;
        json.insert("type",2);
        QJsonObject dataObject;
        dataObject.insert("key",206);
        dataObject.insert("val","0");
        json.insert("data",QJsonValue(dataObject));
        QJsonDocument document;
        document.setObject(json);
        QByteArray data = document.toJson(QJsonDocument::Compact);
        QMQTT::Message msg(0, topic.toLatin1(), data);
        qDebug()<<"alarm_ctrl = "<<data.data();
        client->publish(msg);
    }
}

void Widget::gustroom_wdkz(QString wendu)
{
    QString topic = "/app/control/down";
    QJsonObject json;
    json.insert("type",2);
    QJsonObject dataObject;
    dataObject.insert("key",204);
    dataObject.insert("val",wendu);
    json.insert("data",QJsonValue(dataObject));
    QJsonDocument document;
    document.setObject(json);
    QByteArray data = document.toJson(QJsonDocument::Compact);
    QMQTT::Message msg(0, topic.toLatin1(), data);
    qDebug()<<"alarm_ctrl = "<<data.data();
    client->publish(msg);
}

void Widget::doSubscribed(const QString& topic, const quint8 qos)
{
    qDebug()<<"doSnubscribed = "<<topic;
}

void Widget::doDataReceived(QMQTT::Message message)
{
    QString mes = message.payload();
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(mes.toLatin1(), &jsonError);  // 转化为 JSON 文档
    if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
    {
        if(doucment.isObject())                         // JSON 文档为对象
        {
            QJsonObject object = doucment.object();     // 转化为对象
            if (!object.contains("type") && object.contains("result"))
            {
                Widget::modeup(message);
            }
            else if(object.contains("type"))
            {
                QJsonValue typeval = object.value("type");
                if(1 == typeval.toInt())
                    Widget::dataup(message);
                else if(2 == typeval.toInt())
                    Widget::controlup(message);
            }
            else
            {
                Widget::dataup(message);
            }
        }
    }
}
//上报模式
void Widget::modeup(QMQTT::Message message)
{
    QString mes = message.payload();
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(mes.toLatin1(), &jsonError);  // 转化为 JSON 文档
    QJsonObject object = doucment.object();
    QJsonValue nresult = object.value("result");        // 获取指定 key 对应的 value
    int result = nresult.toInt();
    if(result == 1)
    {
        qDebug()<<"模式修改失败";
        QString buf="Modified failed";
        emit huixian(buf);
    }
    else if(result == 0)
    {
        qDebug()<<"模式修改成功";
        QString buf="Modified successfully";
        emit huixian(buf);
    }
}
void Widget::controlup(QMQTT::Message message)
{
    QString mes = message.payload();
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(mes.toLatin1(), &jsonError);  // 转化为 JSON 文档
    QJsonObject object = doucment.object();
    QJsonValue resultval = object.value("result");
    int result = resultval.toInt();
    if(0 == result)
    {
        QJsonValue nmsg = object.value("msg");
        QString msg = nmsg.toString();
        qDebug()<<"控制成功";
    }
    else if(1 == result)
    {
        QJsonValue nmsg = object.value("msg");
        QString msg = nmsg.toString();
        qDebug()<<"控制失败";
    }
}

void Widget::dataup(QMQTT::Message message)
{
    QString mes = message.payload();
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(mes.toLatin1(), &jsonError);  // 转化为 JSON 文档
    QJsonObject object = doucment.object();  // 转化为对象
    QJsonValue value = object.value("data");
    QJsonArray array = value.toArray();
    for(int i = 0;i < array.size(); i++)
    {
        QJsonValue array_value = array.at(i);
        QJsonObject obj = array_value.toObject();
        QJsonValue keyn = obj.value("key");
        switch (keyn.toInt())
        {
        case 1:{
            QJsonValue nval = obj.value("val");
            //double val = nval.toDouble();
            double val = nval.toVariant().toDouble();
            QString str1 = QString::number(val);
            QString str2 = "℃";
            QString str = str1 + str2;
            emit data1(str);
            qDebug() << "temperature =  " << val;

            break;}
        case 2:{
            QJsonValue nval = obj.value("val");
            //double val = nval.toDouble();
            double val = nval.toVariant().toDouble();
            QString str1 = QString::number(val);
            QString str2 = "%rh";
            QString str = str1 + str2;
            emit data2(str);
            qDebug() << "humidity =  " << val;

            break;}
        case 3:{
            QJsonValue nval = obj.value("val");
            //int val = nval.toInt();
            double val = nval.toVariant().toDouble();
            QString str1 = QString::number(val);
            QString str2 = "Lux";
            QString str = str1 + str2;
            emit data3(str);
            qDebug() << "ill =  " << val;
            break;}
        case 101:{
            QJsonValue nval = obj.value("val");
            double val = nval.toVariant().toDouble();
            QString str1 = QString::number(val);
            QString str2 = "℃";
            QString str = str1 + str2;
            emit data101(str);
            qDebug() << "bedtemperature =  " << val;
            break;}
        case 102:{
            QJsonValue nval = obj.value("val");
            double val = nval.toVariant().toDouble();
            QString str1 = QString::number(val);
            QString str2 = "%rh";
            QString str = str1 + str2;
            emit data102(str);
            qDebug() << "bedhumidity =  " << val;
            break;}
        case 201:{
            QJsonValue nval = obj.value("val");
            //int val = nval.toInt();
            double val = nval.toVariant().toDouble();
            QString str1 = QString::number(val);
            QString str2 = "Lux";
            QString str = str1 + str2;
            emit data201(str);
            qDebug() << "ill =  " << val;
            break;
        }
        case 202:{
            QJsonValue nval = obj.value("val");
            double val = nval.toVariant().toDouble();
            QString str1 = QString::number(val);
            QString str2 = "%rh";
            QString str = str1 + str2;
            emit data202(str);
            qDebug() << "bedhumidity =  " << val;
            break;
        }
        default:
            break;
        }
    }
}

Widget::~Widget()
{
    delete ui;
}
